﻿var zombies = zombies || {};

zombies.publayout = function () {
    var self = this;
    //channel info
    self.ChannelName = ko.observable();
    self.ChannelCover = ko.observable();
    self.About = ko.observable();
    self.PubId = ko.observable();
    self.Address = ko.observable();
    self.Facebook = ko.observable();
    self.LogoLink = ko.observable();
    self.Phone = ko.observable();

    self.Cover = ko.computed(function () {
        return "url(" + self.ChannelCover() + ") no-repeat fixed center top / cover transparent";
    });

    self.doChangeAvatar = function () {
        $("#file_changeAvatar").trigger('click');
    };

    self.doChangeCover = function () {
        $("#file_changeCover").trigger('click');
    };
}

zombies.rightBar = function () {
    var self = this;
    self.Subcribers = ko.observableArray();
    self.Comments = ko.observableArray();
};

ko.bindingHandlers.LogoUpload = {
    update: function (element, valueAccessor, allBindingsAccessor, createAdvViewModel, bindingContext) {
        var options = ko.utils.unwrapObservable(valueAccessor()),
            property = ko.utils.unwrapObservable(options.property),
            url = ko.utils.unwrapObservable(options.url);

        if (property && url) {
            $(element.form).change(function () {
                if ($(element.form).valid() && element.files.length) {
                    var $this = $(element),
                        filename = $this.val();
                    $(element.form).ajaxSubmit({
                        url: url,
                        enctype: "multipart/form-data",
                        type: "POST",
                        dataType: "json",
                        data: { pubid: layoutModel.PubId() },
                        headers: { "Content-Disposition": "attachment; filename=" + filename },
                        beforeSubmit: function () {
                            $(".progress").show();
                            $(".progressError").hide();
                            $(".bar").width("0%")
                            $(".percent").html("0%");
                        },
                        uploadProgress: function (event, position, total, percentComplete) {
                            var percentVal = percentComplete + "%";
                            $(".bar").width(percentVal)
                            $(".percent").html(percentVal);
                        },
                        success: function (data) {
                            $(".progress").hide();
                            $(".progressError").hide();

                            layoutModel.LogoLink(data);
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            $(".progress").hide();
                            $("div.progressError").html(jqXHR.responseText);
                        }
                    });
                }
                return false;
            });
        }
    }
}


ko.bindingHandlers.CoverUpload = {
    update: function (element, valueAccessor, allBindingsAccessor, createAdvViewModel, bindingContext) {
        var options = ko.utils.unwrapObservable(valueAccessor()),
            property = ko.utils.unwrapObservable(options.property),
            url = ko.utils.unwrapObservable(options.url);

        if (property && url) {
            $(element.form).change(function () {
                if ($(element.form).valid() && element.files.length) {
                    var $this = $(element),
                        filename = $this.val();
                    $(element.form).ajaxSubmit({
                        url: url,
                        enctype: "multipart/form-data",
                        type: "POST",
                        dataType: "json",
                        data: { pubid: layoutModel.PubId() },
                        headers: { "Content-Disposition": "attachment; filename=" + filename },
                        beforeSubmit: function () {
                            $(".progress").show();
                            $(".progressError").hide();
                            $(".bar").width("0%")
                            $(".percent").html("0%");
                        },
                        uploadProgress: function (event, position, total, percentComplete) {
                            var percentVal = percentComplete + "%";
                            $(".bar").width(percentVal)
                            $(".percent").html(percentVal);
                        },
                        success: function (data) {
                            $(".progress").hide();
                            $(".progressError").hide();
                            layoutModel.ChannelCover(data);
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            $(".progress").hide();
                            $("div.progressError").html(jqXHR.responseText);
                        }
                    });
                }
                return false;
            });
        }
    }
}


var loadchannel = function () {
    $.ajax({
        type: "GET",
        url: "/api/Publisher/GetCurrentChannel",
        contentType: "application/json",
        dataType: 'json'
    }).done(function (item) {
        layoutModel.PubId(item.PubId);
        layoutModel.ChannelName(item.ChannelName);
        if (item.ChannelCover === null) layoutModel.ChannelCover('../Advertise/DefaultAssets/intro.jpg');
        else layoutModel.ChannelCover(item.ChannelCover);
        layoutModel.About(item.About);
        layoutModel.Address(item.Address);
        layoutModel.LogoLink(item.LogoLink === null ? '../Advertise/DefaultAssets/1.jpg' : item.LogoLink);
        layoutModel.Facebook(item.Facebook);
        layoutModel.Phone(item.Phone);
    }).fail(function () {
        toastr.error('Lỗi :(');
    });
};

var loadSubcribers = function () {
    rightBar.Subcribers.removeAll();
    $.ajax({
        type: "GET",
        url: "/api/Publisher/GetSubcribers",
        contentType: "application/json",
        dataType: 'json'
    }).done(function (items) {
        for (var item in items) {
            if (items.hasOwnProperty(item)) {
                rightBar.Subcribers.push(new zombies.Subcriber(items[item]));
            }
        }
    }).fail(function () {
        toastr.error('Lỗi :(');
    });
};

var loadNewComments = function () {
    rightBar.Comments.removeAll();
    $.ajax({
        type: "GET",
        url: "/api/Publisher/GetNewComments",
        contentType: "application/json",
        dataType: 'json'
    }).done(function (items) {
        for (var item in items) {
            if (items.hasOwnProperty(item)) {
                rightBar.Comments.push(new zombies.Comment(items[item]));
            }
        }
    }).fail(function () {
        toastr.error('Lỗi :(');
    });
};

var layoutModel;
var rightBar;
$(document).ready(function () {
    layoutModel = new zombies.publayout();
    rightBar = new zombies.rightBar();
    loadchannel();
    loadNewComments();
    loadSubcribers();

    ko.applyBindings(layoutModel, document.getElementById("cover_publisher"));
    ko.applyBindings(rightBar, document.getElementById("right_publisher"));
});